package com.emokili.ewa.app.controller;

import com.alibaba.fastjson.TypeReference;
import com.emokili.ewa.app.feign.AppFeignService;
import com.emokili.ewa.app.service.MemberAppService;
import com.emokili.ewa.common.enums.HttpStatusEnum;
import com.emokili.ewa.common.utils.Constant;
import com.emokili.ewa.common.utils.JsonUtils;
import com.emokili.ewa.core.frame.entity.MemberEntity;
import com.emokili.ewa.common.utils.R;
import com.emokili.ewa.common.vo.MemberLoginVo;
import com.emokili.ewa.common.vo.MemberRegisterVo;
import com.emokili.ewa.common.vo.MemberUpdateVo;
import com.emokili.ewa.core.frame.service.MemberService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author yuepanpan
 * @date 2021-05-29
 */
@RestController
@RequestMapping("/v1/app")
public class LoginController {

    @Autowired
    MemberService memberService;
    @Autowired
    private MemberAppService memberAppService;
    @Autowired
    private AppFeignService appFeignService;


    @GetMapping(value = "/recaptcha")
    @ResponseBody
    public R recaptcha(@RequestParam("email") String email){
        MemberRegisterVo vo = new MemberRegisterVo();
        vo.setEmail(email);
        memberService.checkEmailUnique(vo);
        return R.ok();
    }

    @PostMapping("/register")
    @ResponseBody
    public R register(@RequestBody MemberRegisterVo vo) {
        appFeignService.register(vo);
        return R.ok();
    }

    @PostMapping("/login")
    @ResponseBody
    public R login(@RequestBody MemberLoginVo vo){
        //远程登录
        R result = appFeignService.login(vo.getEmail(), vo.getPassword());
        if(HttpStatusEnum.SC_OK.getCode().equals(result.getCode())){
            return R.ok().setData(result.getData("data",new TypeReference<String>(){}));
        }else{
            return R.error(HttpStatusEnum.SC_BAD_REQUEST.getCode(),"Please use your username and password");
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(HttpServletRequest request, @RequestBody MemberUpdateVo memberUpdateVo){
        MemberEntity member = (MemberEntity) request.getAttribute("member");
        if(StringUtils.isNotEmpty(memberUpdateVo.getEmail())
                || StringUtils.isNotEmpty(memberUpdateVo.getCity())
                || StringUtils.isNotEmpty(memberUpdateVo.getFirstName())
                || StringUtils.isNotEmpty(memberUpdateVo.getLastName())
                || StringUtils.isNotEmpty(memberUpdateVo.getLanguage())
                || StringUtils.isNotEmpty(memberUpdateVo.getPassword())){
            memberUpdateVo.setId(member.getId());
            MemberEntity entity = memberService.update(memberUpdateVo);
            return R.ok().setData(entity);
        }else {
            return R.ok();
        }
    }

    @PostMapping("/logout")
    @ResponseBody
    public R logout(HttpServletRequest request){
        String sessionId = (String) request.getSession().getAttribute(Constant.LOGIN_USER);
        R result = appFeignService.logout(sessionId);
        if(HttpStatusEnum.SC_OK.getCode().equals(result.getCode())){
            return R.ok();
        }else {
            return R.error();
        }
    }
}
